package util.preprocessing;

import java.util.Enumeration;
import java.util.Hashtable;

import util.delicious.DmozEntryDescription;
import util.io.FileInput;

/**
 * 
 * @author Sergio Duarte
 * 
 */
public class dmozExtractor {

	/**
	 * Generates a txt with a list of the href of the resources and its
	 * types(Kids, teens, mteens) from a DMOZ-Kids-Teens rdf file
	 * 
	 * @param filename
	 *            Path of the DMOZ-rdf file
	 */
	public static void printKT_URLandType(String filename) {

		FileInput in = new FileInput(filename);

		boolean externalPage = false;

		short kids = 0;
		short teens = 0;
		short mteens = 0;
		String url = null;

		String line = in.readString();

		while (!in.eof()) {

			if (line.contains("<ExternalPage about")) {

				url = extractUrl(line);
				externalPage = true;

			}

			else if (line.contains("<ages>") && externalPage == true) {

				if (line.contains("kids")) {
					kids = 1;

				} else if (line.contains("teens")) {
					teens = 1;

				} else if (line.contains("mteens")) {
					mteens = 1;

				} else {

					System.out.println("Error: Age type not recognized");
					System.exit(1);
				}

			}

			else if (line.contains("</ExternalPage>")) {
				System.out.println(url + "\t" + kids + "\t" + teens + "\t"
						+ mteens);
				url = null;
				kids = 0;
				teens = 0;
				mteens = 0;
				externalPage = false;
			}

			line = in.readString();
		}

	}

	public static Hashtable<String, Integer[]> genKT_URLandType(String filename) {

		FileInput in = new FileInput(filename);

		Hashtable<String, Integer[]> hash = new Hashtable<String, Integer[]>();

		boolean externalPage = false;

		short kids = 0;
		short teens = 0;
		short mteens = 0;
		String url = null;

		String line = in.readString();

		while (!in.eof()) {

			if (line.contains("<ExternalPage about")) {

				url = extractUrl(line);
				externalPage = true;

			}

			else if (line.contains("<ages>") && externalPage == true) {

				if (line.contains("kids")) {
					kids = 1;

				} else if (line.contains("mteens")) {
					mteens = 1;

				} else if (line.contains("teens")) {
					teens = 1;

				} else {

					System.out.println("Error: Age type not recognized");
					System.exit(1);
				}

			} else if (line.contains("</ExternalPage>")) {
				// System.out.println(url + "\t" + kids + "\t" + teens + "\t"+
				// mteens);

				Integer param[] = new Integer[3];

				param[0] = (int) kids;
				param[1] = (int) teens;
				param[2] = (int) mteens;

				hash.put(url, param);

				kids = 0;
				teens = 0;
				mteens = 0;
				externalPage = false;

			}

			line = in.readString();
		}

		return hash;

	}

	public static Hashtable<String, DmozEntryDescription> getURLandInfo(String filename, String filter) {

		FileInput in = new FileInput(filename);
		Hashtable<String, DmozEntryDescription> hash = new Hashtable<String, DmozEntryDescription>();
		boolean externalPage = false;
		String url = null;
		String line = in.readString();
		DmozEntryDescription descriptor = null;
		while (!in.eof()) {
			if (line.contains("<ExternalPage about")) {
				descriptor = new DmozEntryDescription();
				url = extractUrl(line);
				externalPage = true;
			} else if (line.contains("<d:Description>") && externalPage == true) {
				String desc = line.replace("<d:Description>", "");
				desc = desc.replace("/<d:Description>", "");
				desc = desc.replaceFirst("\\s+", "").trim();
				descriptor.description = desc;
			} else if (line.contains("<d:Title>") && externalPage == true) {
				String desc = line.replace("<d:Title>", "");
				desc = desc.replace("/<d:Title>", "");
				desc = desc.replaceFirst("\\s+", "").trim();
				descriptor.title = desc;
			} else if (line.contains("<topic>") && externalPage == true) {
				String desc = line.replace("<topic>", "");
				desc = desc.replace("</topic>", "");
				desc = desc.replaceFirst("\\s+", "").trim();
				descriptor.topic = desc;

			}

			else if (line.contains("<ages>") && externalPage == true) {
				String desc = line.replace("<ages>", "");
				desc = desc.replace("</ages>", "");
				desc = desc.replaceFirst("\\s+", "").trim();

				if (desc.contains("kids"))
					descriptor.kids = true;

				if (desc.contains("teens"))
					descriptor.teens = true;

				if (desc.contains("mteens"))
					descriptor.mteens = true;

			}
			
			else if (line.contains("</ExternalPage>")) {
				// System.out.println(url + "\t" + kids + "\t" + teens + "\t"+
				// mteens);
							
			if(descriptor.topic.startsWith(filter)){
				hash.put(url.trim(), descriptor);
				
				//System.out.println(url.trim());
			}
				descriptor=null;
				
			
				externalPage = false;

			}

			line = in.readString();
		}

		return hash;

	}
	
	
	
	/**

	 * 
	 * @param filename PATH of the DMOZ content file
	 * @param mode 0 (all entries); 1 (only kids) ; 2 (only teens); 3 (only mteens); 4 (kids-teens); 5 (kinds-teens-mteens); 6 (for mteens) 
	 * 			   
	 * @return
	 */
	
	public static Hashtable<String, DmozEntryDescription> getURLandInfo(String filename, int mode) {

		FileInput in = new FileInput(filename);
		Hashtable<String, DmozEntryDescription> hash = new Hashtable<String, DmozEntryDescription>();
		boolean externalPage = false;
		String url = null;
		String line = in.readString();
		DmozEntryDescription descriptor = null;
		while (!in.eof()) {
			if (line.contains("<ExternalPage about")) {
				descriptor = new DmozEntryDescription();
				url = extractUrl(line);
				descriptor.url=url;
				externalPage = true;
			} else if (line.contains("<d:Description>") && externalPage == true) {
				String desc = line.replace("<d:Description>", "");
				desc = desc.replace("/<d:Description>", "");
				desc = desc.replaceFirst("\\s+", "").trim();
				descriptor.description = desc;
			} else if (line.contains("<d:Title>") && externalPage == true) {
				String desc = line.replace("<d:Title>", "");
				desc = desc.replace("/<d:Title>", "");
				desc = desc.replaceFirst("\\s+", "").trim();
				descriptor.title = desc;
			} else if (line.contains("<topic>") && externalPage == true) {
				String desc = line.replace("<topic>", "");
				desc = desc.replace("</topic>", "");
				desc = desc.replaceFirst("\\s+", "").trim();
				descriptor.topic = desc;

			}

			else if (line.contains("<ages>") && externalPage == true) {
				String desc = line.replace("<ages>", "");
				desc = desc.replace("</ages>", "");
				desc = desc.replaceFirst("\\s+", "").trim();

				if (desc.contains("kids"))
					descriptor.kids = true;

				if (desc.contains("teens"))
					descriptor.teens = true;

				if (desc.contains("mteens"))
					descriptor.mteens = true;

			}
			
			else if (line.contains("</ExternalPage>")) {
				// System.out.println(descriptor.topic + "\t" + descriptor.url+ "\t" + determineEntryMode(descriptor));
							
			if(mode == determineEntryMode(descriptor)){
				hash.put(url.trim(), descriptor);
				
				//System.out.println(url.trim());
			}
				descriptor=null;
				
			
				
			
				externalPage = false;

			}

			line = in.readString();
		}

		return hash;

	}

	
	/**
	  
	 * @param desc
	 * @return -1 (no-children); 1 (only kids) ; 2 (only teens); 3 (only mteens); 4 (for kids, teens); 5 (for kids,teens, mteens) 
	 */
	public static int determineEntryMode(DmozEntryDescription desc) {

		if (desc.isOnlyKids())
			return 1;

		if (desc.isOnlyTeens())
			return 2;

		if (desc.isOnlyMTeens())
			return 3;

		if (desc.kids && desc.teens)
			return 5;

		if (desc.kids && desc.teens && desc.mteens)
			return 6;
		
		
		if (!desc.kids && desc.teens && desc.mteens)
			return 7;
		
		
		if (!desc.kids && !desc.teens && !desc.mteens)
			return 0;

		return -1;
	}
	
	private static String extractUrl(String line) {
		// TODO Auto-generated method stub

		String a = line.replace("<ExternalPage about=\"", "");
		a = a.replace("\">", "");

		if (a.endsWith("/")) {

			return a.substring(0, a.length() - 1).trim();
		}

		return a.trim();

	}

	/**
	 * @param args
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String path = "/home/sergio/projects/data/dmoz/kt/kt-content.rdf.u8";
		// path = "/home/sergio/projects/data/dmoz/content.rdf.u8";
		Hashtable<String, DmozEntryDescription> hash = getURLandInfo(path, 1);
		
		Enumeration<String> iter = hash.keys();
		
		while(iter.hasMoreElements()){
			String key = iter.nextElement();
	
			DmozEntryDescription dmoz = hash.get(key);
			
			System.out.println(dmoz.topic.replace("Top/Kids_and_Teens/", "") + "\t"+  dmoz.url + "\t"+ dmoz.title.replace("</d:Title>","") + "\t"+ dmoz.description.replace("</d:Description>",""));
			
		}

	}

}

